package com.relytosoft.mizar.analyze.algorithm.face.bitmain.service.impl;

import com.alibaba.fastjson.JSON;
import com.relytosoft.dubhe.common.core.object.ResponseResult;
import com.relytosoft.mizar.analyze.algorithm.base.feign.bitman.FaceFeignService;
import com.relytosoft.mizar.analyze.algorithm.base.service.TaskService;
import com.relytosoft.mizar.analyze.base.entity.bitman.TaskDto;
import com.relytosoft.mizar.analyze.base.entity.bitman.TaskInputDto;
import com.relytosoft.mizar.analyze.base.entity.bitman.TaskRtspDto;
import com.relytosoft.mizar.analyze.base.entity.pojo.dto.TaskRequestDto;
import com.relytosoft.mizar.analyze.common.utils.BitmanRequestUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Jierry
 * @version 2022/5/10.
 */
@Service
public class TaskServiceImpl implements TaskService {
    @Resource
    private FaceFeignService faceFeignService;

    @Override
    public String test() {
        return "test";
    }

    @Override
    public String createTask(TaskRequestDto taskRequestDto, String statusUploadUrl, String uploadUrl) {
        TaskDto taskDto = getTaskDto(taskRequestDto, uploadUrl, statusUploadUrl);
        String result = BitmanRequestUtil.handlerResponse(faceFeignService.createTask(JSON.toJSONString(taskDto)));
        return JSON.parseObject(result).getString("TaskID");
    }

    @Override
    public ResponseResult stopTask(String taskId) {
        Map<String, Object> bodys = new HashMap<>(1);
        bodys.put("TaskID", taskId);
        String strBodys = JSON.toJSONString(bodys);
        BitmanRequestUtil.handlerResponse(faceFeignService.stopTask(strBodys));
        return ResponseResult.ok();
    }

    /**
     * 转化为第三方请求实体
     */
    private TaskDto getTaskDto(TaskRequestDto taskRequestDto, String uploadUrl, String statusUploadUrl) {
        TaskDto taskDto = new TaskDto();
        taskDto.setServiceType(0);
        taskDto.setTaskType(1);
        taskDto.setUploadUrl(uploadUrl);
        taskDto.setStatusUploadUrl(statusUploadUrl);
        TaskInputDto taskInputDto = new TaskInputDto();
        taskInputDto.setTaskName(taskRequestDto.getTaskName());
        taskInputDto.setSrcId(taskRequestDto.getDeviceId());
        taskInputDto.setDeviceName(taskRequestDto.getDeviceName());
        taskInputDto.setResolution("1920*1080");
        taskInputDto.setTowards(0);
        taskInputDto.setSrcType(1);
        taskInputDto.setEncodeFormat("h264");
        TaskRtspDto taskRtspDto = new TaskRtspDto();
        taskRtspDto.setAddress(taskRequestDto.getRtspUrl());
        taskRtspDto.setProtocolType(1);
        taskInputDto.setRtspSrc(taskRtspDto);
        taskInputDto.setRtspSrc(taskRtspDto);
        taskDto.setInputSrc(taskInputDto);
        return taskDto;
    }
}
